import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.3

Item {
    property int curPage: 1
    property int pageCount: 0
    property int recordCount: 0
    RowLayout {
        anchors.fill: parent
        Button {
            text: "<<"
            Layout.preferredWidth: 30
            Layout.alignment: Qt.AlignCenter
            enabled: curPage == 1 ? false : true
            onClicked: {
                curPage = 1
                freshResult()
            }
        }
        Button {
            text: "<"
            Layout.preferredWidth: 30
            Layout.alignment: Qt.AlignCenter
            enabled: curPage == 1 ? false : true
            onClicked: {
                curPage -= 1
                freshResult()
            }
        }
        Button {
            text: ">"
            Layout.preferredWidth: 30
            enabled: curPage >= pageCount ? false : true
            onClicked: {
                curPage += 1
                freshResult()
            }
        }
        Button {
            text: ">>"
            Layout.preferredWidth: 30
            enabled: curPage >= pageCount ? false : true
            onClicked: {
                curPage = pageCount
                freshResult()
            }
        }

        Text {
            text: "第"
        }
        TextField {
            id: pageIndex
            Layout.preferredWidth: 30
            text: curPage
            validator: RegExpValidator {
                regExp: /\d{1,6}/
            }
        }
        Text {
            text: "页"
        }
        Button {
            text: "跳转"
            onClicked: {
                if (parseInt(pageIndex.text) > pageCount) {
                    tipInput.text = "超过页码范围!"
                    tipInput.open()
                } else {
                    curPage = parseInt(pageIndex.text)
                    freshResult()
                }
            }
        }
        Item {
            Layout.fillWidth: true
        }

        Text {
            id: tipTotalRecored
            text: "共" + recordCount + "条记录"
        }
        Text {
            text: "每页"
        }
        ComboBox {
            id: comPagePer
            model: [20, 40, 80, 160]
            Layout.preferredWidth: 55
            onCurrentIndexChanged: {
                if (recordCount > 0) {
                    //                console.log(" comPagePer change")
                    curPage = 1
                    freshResult()
                    freshPageCount()
                }
            }
        }
        Text {
            text: "条记录"
        }

        Text {
            id: tipTotalPage
            text: "共" + pageCount + "页"
        }
        //        Item {
        //            Layout.preferredWidth: 10
        //        }
    }

    onRecordCountChanged: {
        freshPageCount()
    }

    function getReqPageInfo() {
        var req = "#page_no=" + pageIndex.text
        req += "#page_size=" + comPagePer.currentText
        return req
    }

    function freshPageCount() {
        console.log(" recordCount:" + recordCount + " PagePer:" + comPagePer.currentText)
        if (recordCount > 0) {
            pageCount = recordCount / parseInt(comPagePer.currentText)
            pageCount += (recordCount % parseInt(
                              comPagePer.currentText)) == 0 ? 0 : 1
        } else
            pageCount = 0
    }
}
